﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Adex
{

    public partial class RfController
    {

        public static int AddOrUpdateAdvertiser(ref Advertiser newRec)
        {
            try
            {
                object oldRecID = Advertiser.Query().DISTINCT().SetSelectList("AdvertiserID").WHERE("AdvertiserCode", newRec.AdvertiserCode).AND("MediaType", newRec.MediaType).ExecuteScalar();

                Advertiser oldRec = new Advertiser(oldRecID);
                if (oldRec.AdvertiserID > 0)
                {
                    if (newRec.AdvertiserName != oldRec.AdvertiserName || newRec.Tv != oldRec.Tv || newRec.Rd != oldRec.Rd || newRec.Cb != oldRec.Cb || newRec.Pr != oldRec.Pr || newRec.Od != oldRec.Od || newRec.Ns != oldRec.Ns || newRec.InX != oldRec.InX)
                    {
                        oldRec.Tv = newRec.Tv ? newRec.Tv : oldRec.Tv;
                        oldRec.Rd = newRec.Rd ? newRec.Rd : oldRec.Rd;
                        oldRec.Cb = newRec.Cb ? newRec.Cb : oldRec.Cb;
                        oldRec.Pr = newRec.Pr ? newRec.Pr : oldRec.Pr;
                        oldRec.Od = newRec.Od ? newRec.Od : oldRec.Od;
                        oldRec.Ns = newRec.Ns ? newRec.Ns : oldRec.Ns;
                        oldRec.InX = newRec.InX ? newRec.InX : oldRec.InX;
                        oldRec.AdvertiserName = newRec.AdvertiserName;

                        oldRec.DateModified = DateTime.Now;
                        oldRec.Save();
                        
                    }
                    newRec = oldRec;
                    return oldRec.AdvertiserID;
                     
                }
                else
                {
                    //insert new record
                    newRec.DateAdded = DateTime.Now;
                    newRec.DateModified = DateTime.Now;
                    newRec.Save();
                    return newRec.AdvertiserID;
                }

            }
            catch (Exception ex)
            {
                throw new Exception("AddOrUpdateAdvertiser: Error Saving Record. " + ex.Message + " [AdvertiserCode:" + newRec.AdvertiserCode + " AdvertiserName: " + newRec.AdvertiserName + "]");
            }
        }

        public static int AddOrUpdateAgency(ref Agency newRec)
        {
            try
            {
                object oldRecID = Agency.Query().DISTINCT().SetSelectList("AgencyID").WHERE("AgencyCode", newRec.AgencyCode).AND("MediaType", newRec.MediaType).ExecuteScalar();

                Agency oldRec = new Agency(oldRecID);
                if (oldRec.AgencyID > 0)
                {
                    if (oldRec.AgencyName != newRec.AgencyName || newRec.Tv != oldRec.Tv || newRec.Rd != oldRec.Rd || newRec.Cb != oldRec.Cb || newRec.Pr != oldRec.Pr || newRec.Od != oldRec.Od || newRec.Ns != oldRec.Ns || newRec.InX != oldRec.InX)
                    {
                        oldRec.Tv = newRec.Tv ? newRec.Tv : oldRec.Tv;
                        oldRec.Rd = newRec.Rd ? newRec.Rd : oldRec.Rd;
                        oldRec.Cb = newRec.Cb ? newRec.Cb : oldRec.Cb;
                        oldRec.Pr = newRec.Pr ? newRec.Pr : oldRec.Pr;
                        oldRec.Od = newRec.Od ? newRec.Od : oldRec.Od;
                        oldRec.Ns = newRec.Ns ? newRec.Ns : oldRec.Ns;
                        oldRec.InX = newRec.InX ? newRec.InX : oldRec.InX;
                        oldRec.AgencyName = newRec.AgencyName;
                        oldRec.DateModified = DateTime.Now;
                        oldRec.Save();
                    }
                    newRec = oldRec;
                    return oldRec.AgencyID;

                }
                else
                {
                    //insert new record
                    newRec.DateAdded = DateTime.Now;
                    newRec.DateModified = DateTime.Now;
                    newRec.Save();
                    return newRec.AgencyID;
                }

            }
            catch (Exception ex)
            {
                throw new Exception("AddOrUpdateAgency: Error Saving Record. " + ex.Message + " [AgencyCode:" + newRec.AgencyCode + " AgencyName: " + newRec.AgencyName + "]");
            }
        }

        public static int AddOrUpdateBrand(ref Brand newRec)
        {
             try
            {
                object oldRecID = Brand.Query().DISTINCT().SetSelectList("BrandID").WHERE("BrandCode", newRec.BrandCode).AND("MediaType", newRec.MediaType).ExecuteScalar();

                Brand oldRec = new Brand(oldRecID);
                if (oldRec.BrandID > 0  )
                {
                    if (oldRec.BrandName != newRec.BrandName || newRec.Tv != oldRec.Tv || newRec.Rd != oldRec.Rd || newRec.Cb != oldRec.Cb || newRec.Pr != oldRec.Pr || newRec.Od != oldRec.Od || newRec.Ns != oldRec.Ns || newRec.InX != oldRec.InX)
                    {
                        oldRec.Tv = newRec.Tv ? newRec.Tv : oldRec.Tv;
                        oldRec.Rd = newRec.Rd ? newRec.Rd : oldRec.Rd;
                        oldRec.Cb = newRec.Cb ? newRec.Cb : oldRec.Cb;
                        oldRec.Pr = newRec.Pr ? newRec.Pr : oldRec.Pr;
                        oldRec.Od = newRec.Od ? newRec.Od : oldRec.Od;
                        oldRec.Ns = newRec.Ns ? newRec.Ns : oldRec.Ns;
                        oldRec.InX = newRec.InX ? newRec.InX : oldRec.InX;
                        oldRec.BrandName = newRec.BrandName;
                        oldRec.DateModified = DateTime.Now;
                        oldRec.Save();
                    }

                    newRec = oldRec;
                    return oldRec.BrandID;

                }
                else
                {
                    //insert new record
                    newRec.DateAdded = DateTime.Now;
                    newRec.DateModified = DateTime.Now;
                    newRec.Save();
                    return newRec.BrandID;
                }

            } 
            catch (Exception ex)
            {
                throw new Exception("AddOrUpdateBrand: Error Saving Record. " + ex.Message + " [BrandCode:" + newRec.BrandCode + " BrandName: " + newRec.BrandName + "]");
            }
        }

        public static int AddOrUpdateCategory(ref Category newRec)
        {
            try
            {
                object oldRecID = Category.Query().DISTINCT().SetSelectList("CategoryID").WHERE("CategoryCode", newRec.CategoryCode).AND("MediaType", newRec.MediaType).ExecuteScalar();

                Category oldRec = new Category(oldRecID);
                if (oldRec.CategoryID > 0)
                {
                    if (oldRec.CategoryName != newRec.CategoryName || newRec.Tv != oldRec.Tv || newRec.Rd != oldRec.Rd || newRec.Cb != oldRec.Cb || newRec.Pr != oldRec.Pr || newRec.Od != oldRec.Od || newRec.Ns != oldRec.Ns || newRec.InX != oldRec.InX)
                    {
                        oldRec.Tv = newRec.Tv ? newRec.Tv : oldRec.Tv;
                        oldRec.Rd = newRec.Rd ? newRec.Rd : oldRec.Rd;
                        oldRec.Cb = newRec.Cb ? newRec.Cb : oldRec.Cb;
                        oldRec.Pr = newRec.Pr ? newRec.Pr : oldRec.Pr;
                        oldRec.Od = newRec.Od ? newRec.Od : oldRec.Od;
                        oldRec.Ns = newRec.Ns ? newRec.Ns : oldRec.Ns;
                        oldRec.InX = newRec.InX ? newRec.InX : oldRec.InX;
                        oldRec.CategoryName = newRec.CategoryName;
                        oldRec.DateModified = DateTime.Now;
                        oldRec.Save();
                    }
                    newRec = oldRec;
                    return oldRec.CategoryID;

                }
                else
                {
                    //insert new record
                    newRec.DateAdded = DateTime.Now;
                    newRec.DateModified = DateTime.Now;
                    newRec.Save();
                    return newRec.CategoryID;
                }

            } 
            catch (Exception ex)
            {
                throw new Exception("AddOrUpdateCategory: Error Saving Record. " + ex.Message + " [CategoryCode:" + newRec.CategoryCode + " CategoryName: " + newRec.CategoryName + "]");
            }
        }

        public static bool AddOrUpdateProgram(Program newRec)
        {
            try
            {

                Program oldRec = new Program(newRec.ProgramCode);

                if (oldRec.ProgramID > 0)
                {
                    if (newRec.ProgramName != oldRec.ProgramName || newRec.Tv != oldRec.Tv || newRec.Rd != oldRec.Rd || newRec.Cb != oldRec.Cb)
                        //update record  

                        oldRec.Tv = newRec.Tv ? newRec.Tv : oldRec.Tv;
                    oldRec.Rd = newRec.Rd ? newRec.Rd : oldRec.Rd;
                    oldRec.Cb = newRec.Cb ? newRec.Cb : oldRec.Cb;

                    oldRec.MediaCode = newRec.MediaCode;
                    oldRec.DateModified = DateTime.Now;
                    oldRec.Save();
                }
                else
                {
                    //insert new record
                    newRec.DateAdded = DateTime.Now;
                    newRec.DateModified = DateTime.Now;
                    newRec.Save();
                }

                return true;
            }
            catch (Exception ex)
            {
                throw new Exception("AddOrUpdateProgram: Error Saving Record. " + ex.Message + " [ProgramCode:" + newRec.ProgramCode + " ProgramName: " + newRec.ProgramName + "]");
            }
        }

        public static int AddOrUpdateSector(ref Sector newRec)
        {
            try
            {
                object oldRecID = Sector.Query().DISTINCT().SetSelectList("SectorID").WHERE("SectorCode", newRec.SectorCode).AND("MediaType", newRec.MediaType).ExecuteScalar();

                Sector oldRec = new Sector(oldRecID);
                if (oldRec.SectorID > 0)
                {
                    if (oldRec.SectorName != newRec.SectorName || newRec.Tv != oldRec.Tv || newRec.Rd != oldRec.Rd || newRec.Cb != oldRec.Cb || newRec.Pr != oldRec.Pr || newRec.Od != oldRec.Od || newRec.Ns != oldRec.Ns || newRec.InX != oldRec.InX)
                    {
                        oldRec.Tv = newRec.Tv ? newRec.Tv : oldRec.Tv;
                        oldRec.Rd = newRec.Rd ? newRec.Rd : oldRec.Rd;
                        oldRec.Cb = newRec.Cb ? newRec.Cb : oldRec.Cb;
                        oldRec.Pr = newRec.Pr ? newRec.Pr : oldRec.Pr;
                        oldRec.Od = newRec.Od ? newRec.Od : oldRec.Od;
                        oldRec.Ns = newRec.Ns ? newRec.Ns : oldRec.Ns;
                        oldRec.InX = newRec.InX ? newRec.InX : oldRec.InX;
                        oldRec.SectorName = newRec.SectorName;
                        oldRec.DateModified = DateTime.Now;
                        oldRec.Save();
                    }
                    newRec = oldRec;
                    return oldRec.SectorID;

                }
                else
                {
                    //insert new record
                    newRec.DateAdded = DateTime.Now;
                    newRec.DateModified = DateTime.Now;
                    newRec.Save();
                    return newRec.SectorID;
                }

            } 
            catch (Exception ex)
            {
                throw new Exception("AddOrUpdateSector: Error Saving Record. " + ex.Message + " [SectorCode:" + newRec.SectorCode + " SectorName: " + newRec.SectorName + "]");
            }
        }

        public static int AddOrUpdateSubSector(ref SubSector newRec)
        {
            try
            {
                object oldRecID = SubSector.Query().DISTINCT().SetSelectList("SubSectorID").WHERE("SubSectorCode", newRec.SubSectorCode).AND("MediaType", newRec.MediaType).ExecuteScalar();

                SubSector oldRec = new SubSector(oldRecID);
                if (oldRec.SubSectorID > 0)
                {
                    if (oldRec.SubSectorName != newRec.SubSectorName || newRec.Tv != oldRec.Tv || newRec.Rd != oldRec.Rd || newRec.Cb != oldRec.Cb || newRec.Pr != oldRec.Pr || newRec.Od != oldRec.Od || newRec.Ns != oldRec.Ns || newRec.InX != oldRec.InX)
                    {
                        oldRec.Tv = newRec.Tv ? newRec.Tv : oldRec.Tv;
                        oldRec.Rd = newRec.Rd ? newRec.Rd : oldRec.Rd;
                        oldRec.Cb = newRec.Cb ? newRec.Cb : oldRec.Cb;
                        oldRec.Pr = newRec.Pr ? newRec.Pr : oldRec.Pr;
                        oldRec.Od = newRec.Od ? newRec.Od : oldRec.Od;
                        oldRec.Ns = newRec.Ns ? newRec.Ns : oldRec.Ns;
                        oldRec.InX = newRec.InX ? newRec.InX : oldRec.InX;
                        oldRec.SubSectorName = newRec.SubSectorName;
                        oldRec.DateModified = DateTime.Now;
                        oldRec.Save();
                    }
                    newRec = oldRec;
                    return oldRec.SubSectorID;

                }
                else
                {
                    //insert new record
                    newRec.DateAdded = DateTime.Now;
                    newRec.DateModified = DateTime.Now;
                    newRec.Save();
                    return newRec.SubSectorID;
                }

            } 
            catch (Exception ex)
            {
                throw new Exception("AddOrUpdateSubSector: Error Saving Record. " + ex.Message + " [SubSectorCode:" + newRec.SubSectorCode + " SubSectorName: " + newRec.SubSectorName + "]");
            }
        }

        public static bool AddOrUpdateTypeOfBuy(TypeOfBuy newRec)
        {
            try
            {
                TypeOfBuy oldRec = new TypeOfBuy(newRec.TypeOfBuyCode);

                if (oldRec.TypeOfBuyCode > 0)
                {
                    if (newRec.TypeOfBuyName != oldRec.TypeOfBuyName)
                    {
                        //update record 
                        oldRec.TypeOfBuyName = newRec.TypeOfBuyName;
                        oldRec.DateModified = DateTime.Now;
                        oldRec.Save();
                    }
                }

                else
                {
                    //insert new record
                    newRec.DateAdded = DateTime.Now;
                    newRec.DateModified = DateTime.Now;
                    newRec.Save();
                }

                return true;
            }
            catch (Exception ex)
            {
                throw new Exception("AddOrUpdateTypeOfBuy: Error Saving Record. " + ex.Message + " [TypOfBuyCode:" + newRec.TypeOfBuyCode + " TypOfBuyName: " + newRec.TypeOfBuyName + "]");
            }
        }

        public static int AddOrUpdateProduct(ref Product newRec, int mediaTypeCode, DateTime rfDate)
        {

            try
            {
                
                object oldRecID = Product.Query().DISTINCT().SetSelectList("ProductID").WHERE("ProductCode", newRec.ProductCode).AND("MediaType", newRec.MediaType).ExecuteScalar();
                //object oldRecID = Product.Query().DISTINCT().SetSelectList("ProductID").WHERE("ProductCode", newRec.ProductCode).AND("MediaType", SubSonic.Comparison.Is, null).OR("MediaType", newRec.MediaType).ExecuteScalar();
              
                Product oldRec = new Product(oldRecID);
                if (oldRec.ProductID > 0)
                { 
                        oldRec.ProductName = newRec.ProductName;
                        oldRec.DateModified = DateTime.Now;
                        oldRec.BrandID = newRec.BrandID;
                        
                        switch (mediaTypeCode)
                        {
                            case 1:
                                {
                                    if (oldRec.DateAddedTv > rfDate)
                                    {
                                        oldRec.DateAddedTv = rfDate;
                                    }
                                    break;
                                }
                            case 3:
                                {
                                    if (oldRec.DateAddedRd > rfDate)
                                    {
                                        oldRec.DateAddedRd = rfDate;
                                    }
                                    break;
                                }
                            case 5:
                                {
                                    if (oldRec.DateAddedCb > rfDate)
                                    {
                                        oldRec.DateAddedCb = rfDate;
                                    }
                                    break;
                                }
                            case 6:
                                {
                                    if (oldRec.DateAddedPr > rfDate)
                                    {
                                        oldRec.DateAddedPr = rfDate;
                                    }
                                    break;
                                }
                            case 7:
                                {
                                    if (oldRec.DateAddedOd > rfDate)
                                    {
                                        oldRec.DateAddedOd = rfDate;
                                    }
                                    break;
                                }
                            case 8:
                                {
                                    if (oldRec.DateAddedNs > rfDate)
                                    {
                                        oldRec.DateAddedNs = rfDate;
                                    }
                                    break;
                                }

                            case 9:
                                {
                                    if (oldRec.DateAddedIn > rfDate)
                                    {
                                        oldRec.DateAddedIn = rfDate;
                                    }
                                    break;
                                }
                        }

                        oldRec.Save();
                        newRec = oldRec;
                    return oldRec.ProductID;
                    
                }
                else
                {
                    //insert new record
                    newRec.DateAdded = DateTime.Now;
                    newRec.DateModified = DateTime.Now;

                     switch (mediaTypeCode)
                    {
                        case 1:
                            {
                                newRec.DateAddedTv = rfDate;
                                break;
                            }
                        case 3:
                            {
                                newRec.DateAddedRd = rfDate;
                                break;
                            }
                        case 5:
                            {
                                newRec.DateAddedCb = rfDate;
                                break;
                            }
                        case 6:
                            {
                                newRec.DateAddedPr = rfDate;
                                break;
                            }
                        case 7:
                            {
                                newRec.DateAddedOd = rfDate;
                                break;
                            }
                        case 8:
                            {
                                newRec.DateAddedNs = rfDate;
                                break;
                            }
                        case 9:
                            {
                                newRec.DateAddedIn = rfDate;
                                break;
                            }

                    }//end switch

                    newRec.Save();
                    return newRec.ProductID;
                }

            }  
            catch (Exception ex)
            {
                throw new Exception("AddOrUpdateProduct: Error Saving Record. " + ex.Message + " [ProductCode:" + newRec.ProductCode + " ProductName: " + newRec.ProductName + "]");
            }
        }


        public static int AddOrUpdateProductNews(ref Product newRec, int mediaTypeCode, DateTime rfDate)
        {

            try
            {

               // object oldRecID = Product.Query().DISTINCT().SetSelectList("ProductID").WHERE("ProductCode", newRec.ProductCode).AND("MediaType", newRec.MediaType).ExecuteScalar();
                object oldRecID = Product.Query().DISTINCT().SetSelectList("ProductID").WHERE("ProductCode", newRec.ProductCode).AND("MediaType", SubSonic.Comparison.Is, null).OR("MediaType", newRec.MediaType).ExecuteScalar();

                Product oldRec = new Product(oldRecID);
                if (oldRec.ProductID > 0)
                {
                    oldRec.ProductName = newRec.ProductName;
                    oldRec.DateModified = DateTime.Now;
                    oldRec.BrandID = newRec.BrandID;

                    switch (mediaTypeCode)
                    {
                        case 1:
                            {
                                if (oldRec.DateAddedTv > rfDate)
                                {
                                    oldRec.DateAddedTv = rfDate;
                                }
                                break;
                            }
                        case 3:
                            {
                                if (oldRec.DateAddedRd > rfDate)
                                {
                                    oldRec.DateAddedRd = rfDate;
                                }
                                break;
                            }
                        case 5:
                            {
                                if (oldRec.DateAddedCb > rfDate)
                                {
                                    oldRec.DateAddedCb = rfDate;
                                }
                                break;
                            }
                        case 6:
                            {
                                if (oldRec.DateAddedPr > rfDate)
                                {
                                    oldRec.DateAddedPr = rfDate;
                                }
                                break;
                            }
                        case 7:
                            {
                                if (oldRec.DateAddedOd > rfDate)
                                {
                                    oldRec.DateAddedOd = rfDate;
                                }
                                break;
                            }
                        case 8:
                            {
                                if (oldRec.DateAddedNs > rfDate)
                                {
                                    oldRec.DateAddedNs = rfDate;
                                }
                                break;
                            }
                    }

                    oldRec.Save();
                    newRec = oldRec;
                    return oldRec.ProductID;

                }
                else
                {
                    //insert new record
                    newRec.DateAdded = DateTime.Now;
                    newRec.DateModified = DateTime.Now;

                    switch (mediaTypeCode)
                    {
                        case 1:
                            {
                                newRec.DateAddedTv = rfDate;
                                break;
                            }
                        case 3:
                            {
                                newRec.DateAddedRd = rfDate;
                                break;
                            }
                        case 5:
                            {
                                newRec.DateAddedCb = rfDate;
                                break;
                            }
                        case 6:
                            {
                                newRec.DateAddedPr = rfDate;
                                break;
                            }
                        case 7:
                            {
                                newRec.DateAddedOd = rfDate;
                                break;
                            }
                        case 8:
                            {
                                newRec.DateAddedNs = rfDate;
                                break;
                            }

                    }//end switch

                    newRec.Save();
                    return newRec.ProductID;
                }

            }
            catch (Exception ex)
            {
                throw new Exception("AddOrUpdateProductNews: Error Saving Record. " + ex.Message + " [ProductCode:" + newRec.ProductCode + " ProductName: " + newRec.ProductName + "]");
            }
        }


        public static int AddOrUpdateProdVersion(ref ProdVersion newRec, int mediaTypeCode, DateTime rfDate)
        {
             try
            {
                object oldRecID = ProdVersion.Query().DISTINCT().SetSelectList("ProdVersionID").WHERE("ProdVersionCode", newRec.ProdVersionCode).AND("MediaType", newRec.MediaType).ExecuteScalar();

                ProdVersion oldRec = new ProdVersion(oldRecID);
                if (oldRec.ProdVersionID > 0)
                {
                        oldRec.ProdVersionName = newRec.ProdVersionName;
                        oldRec.DateModified = DateTime.Now;
                        oldRec.BrandID = newRec.BrandID;
                        oldRec.ProductID = newRec.ProductID; 
                        oldRec.ShortVideo = newRec.ShortVideo;
                        oldRec.ShortVideoFile = newRec.ShortVideoFile;
                        oldRec.FullVideo = newRec.FullVideo;
                        oldRec.FullVideoFile = newRec.FullVideoFile;
                        oldRec.RadioFile = newRec.RadioFile;
                        oldRec.CableFile = newRec.CableFile;
                        oldRec.PressFile = newRec.PressFile;
                        oldRec.OutDoorFile = newRec.OutDoorFile;
                        oldRec.Duration = newRec.Duration;
                        oldRec.DateModified = DateTime.Now;
                        oldRec.InternetFile = newRec.InternetFile;
                        
                        switch (mediaTypeCode)
                        {
                            case 1:
                                {
                                    if (oldRec.DateAddedTv > rfDate)
                                    {
                                        oldRec.DateAddedTv = rfDate;
                                    }
                                    break;
                                }
                            case 3:
                                {
                                    if (oldRec.DateAddedRd > rfDate)
                                    {
                                        oldRec.DateAddedRd = rfDate;
                                    }
                                    break;
                                }
                            case 5:
                                {
                                    if (oldRec.DateAddedCb > rfDate)
                                    {
                                        oldRec.DateAddedCb = rfDate;
                                    }
                                    break;
                                }
                            case 6:
                                {
                                    if (oldRec.DateAddedPr > rfDate)
                                    {
                                        oldRec.DateAddedPr = rfDate;
                                    }
                                    break;
                                }
                            case 7:
                                {
                                    if (oldRec.DateAddedOd > rfDate)
                                    {
                                        oldRec.DateAddedOd = rfDate;
                                    }
                                    break;
                                }
                            case 8:
                                {
                                    if (oldRec.DateAddedNs > rfDate)
                                    {
                                        oldRec.DateAddedNs = rfDate;
                                    }
                                    break;
                                }
                            case 9:
                                {
                                    if (oldRec.DateAddedIn > rfDate)
                                    {
                                        oldRec.DateAddedIn = rfDate;
                                    }
                                    break;
                                }
                        }

                        oldRec.Save();
                        newRec = oldRec;
                        return oldRec.ProdVersionID;
                    
                }
                else
                {
                    //insert new record
                    newRec.DateAdded = DateTime.Now;
                    newRec.DateModified = DateTime.Now;

                     switch (mediaTypeCode)
                    {
                        case 1:
                            {
                                newRec.DateAddedTv = rfDate;
                                break;
                            }
                        case 3:
                            {
                                newRec.DateAddedRd = rfDate;
                                break;
                            }
                        case 5:
                            {
                                newRec.DateAddedCb = rfDate;
                                break;
                            }
                        case 6:
                            {
                                newRec.DateAddedPr = rfDate;
                                break;
                            }
                        case 7:
                            {
                                newRec.DateAddedOd = rfDate;
                                break;
                            }
                        case 8:
                            {
                                newRec.DateAddedNs = rfDate;
                                break;
                            }

                    }//end switch

                    newRec.Save();
                    return newRec.ProdVersionID;
                }

            }  
            
            catch (Exception ex)
            {
                throw new Exception("AddOrUpdateVersion: Error Saving Record. " + ex.Message + " [ProdVersionCode:" + newRec.ProdVersionCode + " ProdVersionName: " + newRec.ProdVersionName + "]");
            }
        }

        public static bool AddOrUpdatePressBody(PressBody newRec)
        {
            try
            {
                PressBody oldRec = new PressBody(newRec.PressBodyCode);
                if (oldRec.PressBodyCode > 0)
                {
                    if (newRec.PressBodyName != oldRec.PressBodyName)
                    {
                        //update record 

                        oldRec.PressBodyName = newRec.PressBodyName;

                        oldRec.DateModified = DateTime.Now;
                        oldRec.Save();
                    }
                }
                else
                {
                    //insert new record
                    newRec.DateAdded = DateTime.Now;
                    newRec.DateModified = DateTime.Now;
                    newRec.Save();
                }

                return true;
            }
            catch (Exception ex)
            {
                throw new Exception("AddOrUpdatePressBody: Error Saving Record. " + ex.Message + " [PressBodyCode:" + newRec.PressBodyCode + " PressBodyName: " + newRec.PressBodyName + "]");
            }
        }



        public static bool AddOrUpdateInternetBody(InternetBody newRec)
        {
            try
            {
                InternetBody oldRec = new InternetBody(newRec.InternetBodyCode);
                if (oldRec.InternetBodyCode > 0)
                {
                    if (newRec.InternetBodyName != oldRec.InternetBodyName)
                    {
                        //update record 

                        oldRec.InternetBodyName = newRec.InternetBodyName;

                        oldRec.DateModified = DateTime.Now;
                        oldRec.Save();
                    }
                }
                else
                {
                    //insert new record
                    newRec.DateAdded = DateTime.Now;
                    newRec.DateModified = DateTime.Now;
                    newRec.Save();
                }

                return true;
            }
            catch (Exception ex)
            {
                throw new Exception("AddOrUpdateInternetBody: Error Saving Record. " + ex.Message + " [InternetBodyCode:" + newRec.InternetBodyCode + " InternetBodyName: " + newRec.InternetBodyName + "]");
            }
        }



        public static bool AddOrUpdatePressColor(PressColor newRec)
        {
            try
            {
                PressColor oldRec = new PressColor(newRec.ColorCode);
                if (oldRec.ColorCode > 0)
                {
                    if (newRec.ColorName != oldRec.ColorName)
                    {
                        //update record

                        oldRec.ColorName = newRec.ColorName;
                        oldRec.DateModified = DateTime.Now;
                        oldRec.Save();
                    }
                }
                else
                {
                    //insert new record
                    newRec.DateAdded = DateTime.Now;
                    newRec.DateModified = DateTime.Now;
                    newRec.Save();
                }

                return true;
            }
            catch (Exception ex)
            {
                throw new Exception("AddOrUpdatePressColor: Error Saving Record. " + ex.Message + " [PressColorCode:" + newRec.ColorCode + " PressColorName: " + newRec.ColorName + "]");
            }
        }


        public static bool AddOrUpdateInternetColor(InternetColor newRec)
        {
            try
            {
                InternetColor oldRec = new InternetColor(newRec.ColorCode);
                if (oldRec.ColorCode > 0)
                {
                    if (newRec.ColorName != oldRec.ColorName)
                    {
                        //update record

                        oldRec.ColorName = newRec.ColorName;
                        oldRec.DateModified = DateTime.Now;
                        oldRec.Save();
                    }
                }
                else
                {
                    //insert new record
                    newRec.DateAdded = DateTime.Now;
                    newRec.DateModified = DateTime.Now;
                    newRec.Save();
                }

                return true;
            }
            catch (Exception ex)
            {
                throw new Exception("AddOrUpdateInternetColor: Error Saving Record. " + ex.Message + " [InternetColorCode:" + newRec.ColorCode + " InternetColorName: " + newRec.ColorName + "]");
            }
        }



        public static bool AddOrUpdateVisibility(Visibility newRec)
        {
            try
            {
                Visibility oldRec = new Visibility(newRec.VisibilityCode);

                if (oldRec.VisibilityCode > 0)
                {
                    if (newRec.VisibilityName != oldRec.VisibilityName)
                    {
                        //update record

                        oldRec.VisibilityName = newRec.VisibilityName;

                        oldRec.DateModified = DateTime.Now;
                        oldRec.Save();
                    }
                }
                else
                {
                    //insert new record
                    newRec.DateAdded = DateTime.Now;
                    newRec.DateModified = DateTime.Now;
                    newRec.Save();
                }

                return true;
            }
            catch (Exception ex)
            {
                throw new Exception("AddOrUpdateVisibility: Error Saving Record. " + ex.Message + " [VisibilityCode:" + newRec.VisibilityCode + " VisibilityName: " + newRec.VisibilityName + "]");
            }
        }

        public static bool AddOrUpdateNewsSection(NewsSection newRec)
        {
            try
            {
                NewsSection oldRec = new NewsSection(newRec.SectionCode);
                if (oldRec.SectionCode > 0)
                {
                    if (newRec.SectionName != oldRec.SectionName)
                    {
                        //update record 

                        oldRec.SectionName = newRec.SectionName;

                        oldRec.DateModified = DateTime.Now;
                        oldRec.Save();
                    }
                }
                else
                {
                    //insert new record
                    newRec.DateAdded = DateTime.Now;
                    newRec.DateModified = DateTime.Now;
                    newRec.Save();
                }

                return true;
            }
            catch (Exception ex)
            {
                throw new Exception("AddOrUpdatePressBody: Error Saving Record. " + ex.Message + " [SectionCode:" + newRec.SectionCode + " SectionName: " + newRec.SectionName + "]");
            }
        }


        public static bool AddOrUpdateCarrier(Carrier newRec)
        {
            try
            {
                Carrier oldRec = new Carrier(newRec.CarrierCode);
                if (oldRec.CarrierCode > 0)
                {
                    if (newRec.CarrierName != oldRec.CarrierName)
                    {
                        //update record

                        oldRec.CarrierName = newRec.CarrierName;

                        oldRec.DateModified = DateTime.Now;
                        oldRec.Save();
                    }
                }
                else
                {
                    //insert new record
                    newRec.DateAdded = DateTime.Now;
                    newRec.DateModified = DateTime.Now;
                    newRec.Save();
                }

                return true;
            }
            catch (Exception ex)
            {
                throw new Exception("AddOrUpdateCarrier: Error Saving Record. " + ex.Message + " [CarrierCode:" + newRec.CarrierCode + " CarrierName: " + newRec.CarrierName + "]");
            }
        }

        public static int GetNewProductCodeForVersion(int brandCode, int versionCode, string versionName, int mediaTypeCode, DateTime rfDate)
        {
            Product prod = new Product();
            prod.ProductCode = versionCode;
            prod.ProductName = versionName;
            prod.BrandID = brandCode;


           return AddOrUpdateProduct(ref prod, mediaTypeCode, rfDate);
             

        }

        public static bool DeleteAllCommercialByDate(DateTime rfDate, int countryID)
        {

            SubSonic.DeleteQuery delSql = new SubSonic.DeleteQuery(Adex.CommercialLog.Schema.TableName);
            delSql.WHERE("RFDate", rfDate).AND("CountryID", countryID);

            try
            {
                delSql.Execute();
                return true;
            }
            catch (Exception ex)
            {
                throw new Exception("DeleteAllCommercialByDate: Error in deleting Commercial Log by Date. " + ex.Message);
            }
        }

        public static bool DeleteAllNewCommercialByDate(DateTime rfDate, int countryID)
        {

            SubSonic.DeleteQuery delSql = new SubSonic.DeleteQuery(Adex.NewCommercialLog.Schema.TableName);
            delSql.WHERE("RFDate", rfDate).AND("CountryID", countryID);

            try
            {
                delSql.Execute();
                return true;
            }
            catch (Exception ex)
            {
                throw new Exception("DeleteAllNewCommercialByDate: Error in deleting Commercial Log by Date. " + ex.Message);
            }
        }


        public static bool DeleteAllPressByDate(DateTime rfDate, int countryID)
        {

            SubSonic.DeleteQuery delSql = new SubSonic.DeleteQuery(Adex.PressLog.Schema.TableName);
            delSql.WHERE("RFDate", rfDate).AND("CountryID", countryID);

            try
            {
                delSql.Execute();
                return true;
            }
            catch (Exception ex)
            {
                throw new Exception("DeleteAllPressByDate: Error in deleting Press Log by Date. " + ex.Message);
            }
        }

        public static bool DeleteAllOutDoorByDate(DateTime rfDate, int countryID)
        {

            SubSonic.DeleteQuery delSql = new SubSonic.DeleteQuery(Adex.OutdoorLog.Schema.TableName);
            delSql.WHERE("RFDate", rfDate).AND("CountryID", countryID);

            try
            {
                delSql.Execute();
                return true;
            }
            catch (Exception ex)
            {
                throw new Exception("DeleteAllOutDoorByDate: Error in deleting Outdoor Log by Date. " + ex.Message);
            }
        }


        public static bool DeleteAllPlanning()
        {

            SubSonic.DeleteQuery delSql = new SubSonic.DeleteQuery(Adex.PlanningLog.Schema.TableName);
            try
            {
                delSql.Execute();
                return true;
            }
            catch (Exception ex)
            {
                throw new Exception("DeleteAllPlanning: Error in deleting Planning Log. " + ex.Message);
            }
        }


        public static bool DeleteAllTraffic()
        {

            SubSonic.DeleteQuery delSql = new SubSonic.DeleteQuery(Adex.TblTrafficLog.Schema.TableName);
            try
            {
                delSql.Execute();
                return true;
            }
            catch (Exception ex)
            {
                throw new Exception("DeleteAllPlanning: Error in deleting Planning Log. " + ex.Message);
            }
        }



        public static bool DeleteAllNewsByDate(DateTime rfDate, int countryID)
        {

            SubSonic.DeleteQuery delSql = new SubSonic.DeleteQuery(Adex.NewsLog.Schema.TableName);
            delSql.WHERE("RFDate", rfDate).AND("CountryID", countryID);

            try
            {
                delSql.Execute();
                return true;
            }
            catch (Exception ex)
            {
                throw new Exception("DeleteAllNewsByDate: Error in deleting News Log by Date. " + ex.Message);
            }
        }

        public static void DeleteAllMediaByDateBetween(int countryID, DateTime sDate, DateTime eDate, int MediaTypeID, int ConditionAllDelete)
        {
            SPs.DeleteAllMediaByDateBetween(countryID, sDate, eDate, MediaTypeID,ConditionAllDelete).Execute();
        }

        public static Int32 AdminChangePassword(String UserID, String CurrentPassword, String NewPassword)
        {
            return Convert.ToInt32(SPs.AdminChangePassword(UserID, CurrentPassword, NewPassword).ExecuteScalar());
        }

        #region[Internet]
        public static bool DeleteAllInternetByDate(DateTime rfDate, int countryID)
        {

            SubSonic.DeleteQuery delSql = new SubSonic.DeleteQuery(Adex.InternetLog.Schema.TableName);
            delSql.WHERE("RFDate", rfDate).AND("CountryID", countryID);

            try
            {
                delSql.Execute();
                return true;
            }
            catch (Exception ex)
            {
                throw new Exception("DeleteAllInternetByDate: Error in deleting Internet Log by Date. " + ex.Message);
            }
        }
         #endregion

        public static DataTable GetVideosAudiosCategory(int? MediaType, int? CategoryID)
        {
            return SPs.GetVideosAudiosCategory(MediaType, CategoryID).GetDataSet().Tables[0];
        }

        public static DataTable GetVideosAudiosCategoryName(int? MediaType, string CategoryName)
        {
            return SPs.GetVideosAudiosCategoryName(MediaType, CategoryName).GetDataSet().Tables[0];
        }

        public static DataTable GetVideosAudiosProductName(int? MediaType, string ProductName)
        {
            return SPs.GetVideosAudiosProductName(MediaType, ProductName).GetDataSet().Tables[0];
        }

        public static DataTable GetVideosAudiosAdvertiserName(int? MediaType, string AdvertiserName)
        {
            return SPs.GetVideosAudiosAdvertiserName(MediaType, AdvertiserName).GetDataSet().Tables[0];
        }


        public static Int32 GetSiteUsers_ModuleStatus(String UserID, Int32 ModuleID)
        {
            return Convert.ToInt32(SPs.GetSiteUsers_ModuleStatus(UserID, ModuleID).ExecuteScalar());
        }

        public static String GetSiteUsers_ModuleStatusAll(String UserID)
        {
            return Convert.ToString(SPs.GetSiteUsers_ModuleStatusAll(UserID).ExecuteScalar());
        }

        public static DataTable GetLastAvailable_All()
        {
            return SPs.GetLastAvailable_All().GetDataSet().Tables[0];
        }

        public static void SiteUser_Insert(Int32 SiteUserID, String IpAddress)
        {
            SPs.SiteUser_Insert(SiteUserID,IpAddress).Execute();
        }

        public static DataTable SiteUser_Detail(Int32 SiteUserID)
        {
           return SPs.SiteUser_Detail(SiteUserID).GetDataSet().Tables[0];
        }


        public static DataTable GetVideosAudiosAdvertiser(int? MediaType, int? AdvertiserID)
        {
            return SPs.GetVideosAudiosAdvertiser(MediaType, AdvertiserID).GetDataSet().Tables[0];
        }
        public static DataTable GetVideosAudiosProduct(int? MediaType, int? ProductID)
        {
            return SPs.GetVideosAudiosProduct(MediaType, ProductID).GetDataSet().Tables[0];
        }
        public static DataTable GetPlanningMediaCodes()
        {
            return SPs.GetPlanningMediaCodes().GetDataSet().Tables[0];
        }
        public static DataTable GetPlanning_SelectedMedia( string MediaCode)
        {
            return SPs.GetPlanning_SelectedMedia(MediaCode).GetDataSet().Tables[0];
        }

      

        public static DataTable GetPlanning_SelectedBillBordsTypeOfCarriers(string MediaCode,string Carriers)
        {
            return SPs.GetPlanning_SelectedBillBordsTypeOfCarriers(MediaCode, Carriers).GetDataSet().Tables[0];
        }

        public static DataTable GetCompetitionBrands_CategoryName(String CategoryName)
        {
            return SPs.GetCompetitionBrands_CategoryName(CategoryName).GetDataSet().Tables[0];
        }

        public static DataTable GetCompetitionAdvertisers_CategoryName(String CategoryName)
        {
            return SPs.GetCompetitionBrands_CategoryName(CategoryName).GetDataSet().Tables[1];
        }
    }
}
